package Presenter;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Model.Game;
import Model.Player;
import View.MapScreen;
import View.SetupScreens.SetupScreenContainer;

/**
 * Constructs the main JFrame for the game, handles the setup screens.
 * 
 * @author Emily
 * 
 */
public class SetupScreenPresenter {
	final JFrame myFrame;
	final Game game;
	final CardLayout gameScreens;
	public final SetupScreenContainer allScreens;
	final IncrementableInteger screenCount;
	MainGameScreen mainGameScreen;

	/**
	 * Constructs the JFrame, instantiates the game, creates a card layout and
	 * the containing JPanel for the setup screens.
	 */
	public SetupScreenPresenter() {

		myFrame = new JFrame("M.U.L.E.");
		game = new Game();
		gameScreens = new CardLayout();
		allScreens = new SetupScreenContainer(game, gameScreens);
		screenCount = new IncrementableInteger(0);

		allScreens.welcome.newGameBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gameScreens.show(allScreens, "Difficulty");
			}
		});
		
		allScreens.welcome.loadGameBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				boolean validName = false;
				while(!validName){
					try{
							
						
							mainGameScreen = new MainGameScreen(game);
							allScreens.add(mainGameScreen, "main");
							gameScreens.addLayoutComponent(mainGameScreen, "main");
							gameScreens.show(allScreens, "main");
							mainGameScreen.grabFocus();
							mainGameScreen.getRound().getTurn().stopTimer();
						
						String gameName = JOptionPane.showInputDialog(this, "Please enter the name of your save file");
						DatabaseTransactions.loadGame(game, mainGameScreen.getRound(), gameName);
						validName = true;
						//mainGameScreen.getLayoutManager().removeLayoutComponent(map);
						MapScreen newMap;
						try {
							newMap = new MapScreen(game);
							mainGameScreen.add(newMap);
							mainGameScreen.getLayoutManager().addLayoutComponent(newMap, "Map1");
							mainGameScreen.getLayoutManager().show(mainGameScreen, "Map1");
							mainGameScreen.repaint();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						mainGameScreen.getLayoutManager().show(mainGameScreen, "Map");
					} catch(SQLException | IOException g) {
						System.out.println(g.getMessage());
					}
				}
				
			}
		});

		allScreens.difficulty.beginner.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent h) {

				try {
					game.setDifficultyBeginner();
					System.out.println("Set difficulty to Beginner");
				} catch (Exception g) {
				}
				gameScreens.show(allScreens, "Map");
				System.out.println("going to map type select screen");
			}
		});

		allScreens.difficulty.normal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent h) {
				try {
					game.setDifficultyStandard();
					System.out.println("Selected the normal difficulty.");
				} catch (Exception g) {
				}
				gameScreens.show(allScreens, "Map");
			}
		});

		allScreens.difficulty.tournament
				.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent h) {

						try {
							game.setDifficultyTournament();
							System.out
									.println("Selected the tournament difficulty.");
						} catch (Exception g) {
						}

						gameScreens.show(allScreens, "Map");
					}
				});

		allScreens.map.random.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent h) {

				try {
					game.setMapTypeRandom();
					System.out.println("Selected the random map");
				} catch (Exception g) {
				}
				gameScreens.show(allScreens, "number");

			}
		});

		allScreens.map.normal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent h) {
				try {
					game.setMapTypeStandard();
					System.out.println("Selected the normal map.");
				} catch (Exception g) {
				}
				gameScreens.show(allScreens, "number");
			}
		});

		allScreens.number.setNumberOfPlayers
				.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Player[] setTheNumPlayersInThisArray = game
								.getPlayers();
						if (allScreens.number.rdbtn1Players.isSelected()) {
							game.setHumanPlayerCount(1);
							setTheNumPlayersInThisArray[0].setIsHuman(true);
							System.out
									.println("Setting number of human players to 1, player 1 set to human");
						} else if (allScreens.number.rdbtn2Players.isSelected()) {
							game.setHumanPlayerCount(2);
							setTheNumPlayersInThisArray[0].setIsHuman(true);
							setTheNumPlayersInThisArray[1].setIsHuman(true);
							System.out
									.println("Setting number of human players to 2, players 1-2 set to human");

						} else if (allScreens.number.rdbtn3Players.isSelected()) {
							game.setHumanPlayerCount(3);
							setTheNumPlayersInThisArray[0].setIsHuman(true);
							setTheNumPlayersInThisArray[1].setIsHuman(true);
							setTheNumPlayersInThisArray[2].setIsHuman(true);
							System.out
									.println("Setting number of human players to 3, players 1-3 set to human");
						} else {
							game.setHumanPlayerCount(4);
							setTheNumPlayersInThisArray[0].setIsHuman(true);
							setTheNumPlayersInThisArray[1].setIsHuman(true);
							setTheNumPlayersInThisArray[2].setIsHuman(true);
							setTheNumPlayersInThisArray[3].setIsHuman(true);
							System.out
									.println("Setting number of human players to 4, players 1-4 set to human");
						}
						game.setPlayers(setTheNumPlayersInThisArray);
						gameScreens.show(allScreens, "race");
					}
				});

		allScreens.race.setPlayerRaceButton
				.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Player[] setPlayerRaceInThisArray = game.getPlayers();

						setPlayerRaceInThisArray[0]
								.setRace((String) allScreens.race.p1comboBox
										.getSelectedItem());
						setPlayerRaceInThisArray[1]
								.setRace((String) allScreens.race.p2comboBox
										.getSelectedItem());
						setPlayerRaceInThisArray[2]
								.setRace((String) allScreens.race.p3comboBox
										.getSelectedItem());
						setPlayerRaceInThisArray[3]
								.setRace((String) allScreens.race.p4comboBox
										.getSelectedItem());

						game.setPlayers(setPlayerRaceInThisArray);

						System.out.println("Setting player 1 race to "
								+ (String) allScreens.race.p1comboBox
										.getSelectedItem());
						System.out.println("Setting player 2 race to "
								+ (String) allScreens.race.p2comboBox
										.getSelectedItem());
						System.out.println("Setting player 3 race to "
								+ (String) allScreens.race.p3comboBox
										.getSelectedItem());
						System.out.println("Setting player 4 race to "
								+ (String) allScreens.race.p4comboBox
										.getSelectedItem());

						gameScreens.show(allScreens, "color");
					}
				});

		allScreens.color.setColorsButton
				.addActionListener(new ActionListener() {

					/**
					 * Method to set the players colors from GUI selections
					 */
					public void actionPerformed(ActionEvent arg0) {
						Player[] thePlayers = game.getPlayers();

						// set colors. Next check for duplicates
						thePlayers[0]
								.setColor((String) allScreens.color.p1comboBox
										.getSelectedItem());
						thePlayers[1]
								.setColor((String) allScreens.color.p2comboBox
										.getSelectedItem());
						thePlayers[2]
								.setColor((String) allScreens.color.p3comboBox
										.getSelectedItem());
						thePlayers[3]
								.setColor((String) allScreens.color.p4comboBox
										.getSelectedItem());

						if (game.duplicateColors() == true) {
							// setting colors manually to prevent problems in
							// gameplay.
							// they can be reset once button is pressed again
							thePlayers[0].setColor("RED");
							thePlayers[1].setColor("BLUE");
							thePlayers[2].setColor("GREEN");
							thePlayers[3].setColor("YELLOW");
							System.out
									.println("Two players have the same color. Colors will be set to the "
											+ "default setting until a valid selection is made.");

							// disable confirm, show warning
							allScreens.color.warningLabel.setVisible(true);
						}

						else {
							// disable warning
							allScreens.color.warningLabel.setVisible(false);

							gameScreens.show(allScreens, "names");
						}

						/* print out player colors */
						System.out.println("Setting player 1 color to "
								+ thePlayers[0].getColor());
						System.out.println("Setting player 2 color to "
								+ thePlayers[1].getColor());
						System.out.println("Setting player 3 color to "
								+ thePlayers[2].getColor());
						System.out.println("Setting player 4 color to "
								+ thePlayers[3].getColor());

					}
				});
		allScreens.names.setNames.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println(game.getHumanPlayerCount());
				if (allScreens.names.player1TB.getText() == ""
						|| allScreens.names.player2TB.getText() == null
						|| allScreens.names.player3TB.getText() == null
						|| allScreens.names.player4TB.getText() == null) {
					allScreens.names.warning.setVisible(true);
				} else {
					Player[] setTheNamesInThisArray = game.getPlayers();
					setTheNamesInThisArray[0]
							.setName(allScreens.names.player1TB.getText());
					setTheNamesInThisArray[1]
							.setName(allScreens.names.player2TB.getText());
					setTheNamesInThisArray[2]
							.setName(allScreens.names.player3TB.getText());
					setTheNamesInThisArray[3]
							.setName(allScreens.names.player4TB.getText());
					game.setPlayers(setTheNamesInThisArray);

					System.out.println("players[0] name = "
							+ allScreens.names.player1TB.getText());
					System.out.println("players[1] name = "
							+ allScreens.names.player2TB.getText());
					System.out.println("players[2] name = "
							+ allScreens.names.player3TB.getText());
					System.out.println("players[3] name = "
							+ allScreens.names.player4TB.getText());

					allScreens.stats.updateStats();
					gameScreens.show(allScreens, "stats");
				}

			}
		});

		allScreens.stats.startGameBtn.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				// gameScreens.show(allScreens, "main");
				MainGameScreen main;
				try {
					main = new MainGameScreen(game);
					allScreens.add(main, "main");
					gameScreens.addLayoutComponent(main, "main");
					
					gameScreens.show(allScreens, "main");
					main.grabFocus();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});

		gameScreens.show(allScreens, "welcome");

		myFrame.add(allScreens);

		myFrame.setSize(new Dimension(1200, 800));
		myFrame.setVisible(true);
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myFrame.setResizable(false);

	}
}
